package DAO.Impl;

import DAO.getDataDAO;
import domain.Events;
import domain.course;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 数据库连接操作层实现类
 * @author: hank
 * @create: 2021-03-28 21:25
 **/
@SuppressWarnings("JavaDoc")
public class getDataDAOImpl implements getDataDAO {
    private final Connection conn;
    private PreparedStatement pstmt;

    public getDataDAOImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public List<course> getCourse(String User) throws SQLException {
        String sql = "SELECT * FROM course WHERE student_id = ?";
        this.pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, Integer.parseInt(User));
        ResultSet rs = this.pstmt.executeQuery();
        List<course> all = new ArrayList<course>();
        while (rs.next()) {
            course vo = new course();
            vo.setCourses(rs.getString(1));
            vo.setInClass(rs.getString(2));
            vo.setTeacher(rs.getString(3));
            vo.setClass_week(rs.getString(4));
            vo.setRoom(rs.getString(5));
            vo.setStart_time(rs.getString(6));
            vo.setLast_time(rs.getString(7));
            vo.setStudent_id(rs.getString(8));
            vo.setStudent_name(rs.getString(9));
            vo.setStudent_year(rs.getString(10));
            vo.setStudent_major(rs.getString(11));
            vo.setWeek(rs.getString(12));
            all.add(vo);
        }
        if(all.isEmpty()){
            return null;
        }else {
            return all;
        }
    }

    @Override
    public List<Events> getEvents(String User) throws SQLException {
        String sql = "SELECT * FROM events WHERE student_id = ? ORDER BY event_id DESC";
        this.pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, Integer.parseInt(User));
        ResultSet rs = this.pstmt.executeQuery();
        List<Events> all = new ArrayList<>();
        while (rs.next()){
            Events vo =new Events();
            vo.setEventsName(rs.getString(1));
            vo.setEventsID(rs.getInt(2));
            vo.setStartTime(rs.getString(3));
            vo.setLastTime(rs.getString(4));
            vo.setDetail(rs.getString(5));
            vo.setStudentID(rs.getString(6));
            vo.setWeek(rs.getString(7));
            vo.setRoom(rs.getString(8));
            all.add(vo);
        }
        if(all.isEmpty()){
            return null;
        }else {
            return all;
        }
    }
}
